package com.hyt.it.ogt.data.service.pj.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.data.mapper.pj.PjDataExamExpertMapper;
import com.hyt.it.ogt.data.mapper.pj.PjDataModeExpertMapper;
import com.hyt.it.ogt.data.model.entity.pj.ExamExpert;
import com.hyt.it.ogt.data.model.entity.pj.ModeExpert;
import com.hyt.it.ogt.data.model.vo.pj.ExamExpertVo;
import com.hyt.it.ogt.data.model.vo.pj.ModeExpertVo;
import com.hyt.it.ogt.data.service.pj.IPjDataModeExpertService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 评卷模式 服务实现类
 * </p>
 *
 * @author liy
 * @since 2022-02-28
 */
@Service
public class PjDataModeExpertServiceImpl extends BaseServiceImpl<PjDataModeExpertMapper,ModeExpert> implements IPjDataModeExpertService {


    @Autowired
    private PjDataModeExpertMapper pjDataModeExpertMapper;

    @Autowired
    private PjDataExamExpertMapper pjDataExamExpertMapper;

    @Override
    public List<ModeExpertVo> queryModeExpertList(String projectId ,String markModel) {
        //评卷模式列表
        QueryWrapper<ModeExpert> wrapper =  new QueryWrapper<>();
        wrapper.lambda().eq(ModeExpert :: getProjectId,projectId);
        wrapper.groupBy(getGroupBy(markModel));
        List<ModeExpert> modeExperts =  pjDataModeExpertMapper.selectList(wrapper);
        if(CollectionUtils.isEmpty(modeExperts)){
            return Collections.EMPTY_LIST;
        }

        //查询评卷专家
        QueryWrapper<ExamExpert> examExpertWrapper =  new QueryWrapper<>();
        examExpertWrapper.lambda().eq(ExamExpert :: getProjectId,projectId);
        List<ExamExpert> examExperts = pjDataExamExpertMapper.selectList(examExpertWrapper);

        //根据评卷模式列获取评卷专家列表
        Map<String,List<ExamExpertVo>> expertMap = null;
        if(!CollectionUtils.isEmpty(examExperts)){
            List<ExamExpertVo>  examExpertVos = new ArrayList<>();
            for (ExamExpert  examExpert :examExperts) {
                ExamExpertVo examExpertVo = new ExamExpertVo();
                BeanUtils.copyProperties(examExpert,examExpertVo);
                examExpertVos.add(examExpertVo);
            }
            expertMap =  examExpertVos.stream().filter(e-> StringUtils.isNotEmpty(e.getModeExpertId())).collect(Collectors.groupingBy(c -> c.getModeExpertId()));
        }
        List<ModeExpertVo>  modeExpertVos = new ArrayList<>(modeExperts.size());
        for (ModeExpert modeExpert :modeExperts) {
            ModeExpertVo vo = new ModeExpertVo();
            BeanUtils.copyProperties(modeExpert,vo);
            if(!CollectionUtils.isEmpty(expertMap) && !CollectionUtils.isEmpty(expertMap.get(modeExpert.getId()))){
                vo.setExamExpertVos(expertMap.get(modeExpert.getId()));
            }
            modeExpertVos.add(vo);
        }
        return modeExpertVos;
    }

    private String getGroupBy(String markModel){
        //0：按科目评卷，1：按批次评卷，2：按考场评卷
        String groupBy = "subject_id";
        if(StringUtils.isEmpty(markModel)){
            return groupBy;
        }
        switch ( markModel ) {
            case "0" :groupBy = "subject_id";break;
            case "1" :groupBy = "batch_id";break;
            case "2" :groupBy = "room_id";break;
            default :groupBy = "subject_id";break;
        }
        return  groupBy;
    }

}
